Conceptual Homework- Chapter 16

How Does It Work?

a- The variation between Zac and Skylar in their vacation-taking (not accounting for year, just average) is calculated as follows:

#Zac mean
(3+7+5)/3
## [1] 5
#Skylar mean
(2+6+10)/3
## [1] 6

The numbers that represent the variation between Zac and Skylar are 5 and 6.

b- To isolate the variation within Zac and Skylar, we’ll be subtracting their mean from their value for each year:

#Zac
3-5
## [1] -2
7-5
## [1] 2
5-5
## [1] 0
#Skylar
2-6
## [1] -4
6-6
## [1] 0
10-6
## [1] 4

c- For this we’ll be comparing their between-individual variance (Skylar’s mean is 1 higher than Zac’s), so the fixed effects estimate will likely give us an answer closed to 2 since Skylar’s happiness point is 2 as opposed to 1

knitr::include_graphics("IMG_4879.png")

Closed by fixed effects: political polarization of city population -> trust size of city -> trust (as well as size of city -> # of events, although that is not the relationship discussed) ethnic diversity of the city -> trust (as well as ethnic diversity of the city -> segregation) segregation of the city -> trust (as well as segregation of the city -> ethnic diversity)

Open: size of events -> trust size of events -> # of events (and vice versa)

a- Within

b- Within

c- Between

d- Between

e- Combination (since the individual is the genre, but we are comparing an album versus all albums of this genre)

f- Between

  1. This has the effect of controlling for the individual because by taking the individual-level mean, you are accounting for differences within an individual at different points in time, so you have controlled time-sensitive aspects for this specific individual.

How Is It Performed?

  1. Within the data we have and “controlling for city effects” (a way of explaining the fixed effect on the city) an increase of one cultural event within a city than is typical for that city is associated with an increase of 3.6 in trust levels than it typically is for that city.

  2. Within the data we have and “controlling” for city and year effects, an increase of one cultural event that is typical for that city is associated with an increase of 2.4 in trust levels than it typically is for that city.

  3. Firstly it is less complicated in analysis (at least from my perspective); secondly, the polynomial term for time wouldn’t fully control for time (or explain the variation caused by time), it would just be able to help predict the results for each given time period.

  4. B (because random effects allow some amount of between variation into the model, and some of the real individual effect is that between variation)

Coding Homework

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plm)
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
library(broom)
library(broom.mixed)
library(modelsummary)
  1. Downloading data
mp <- read.csv('https://raw.githubusercontent.com/NickCH-K/TheEffectAssignments/main/mathpnl.csv')
mp <- mp %>% 
  select(c('distid', 'year', 'math4', 'expp', 'lunch'))
#number of individuals as N, can get this from variable distid
nrow(unique(mp['distid']))
## [1] 550
#number of time periods as T, can get this from variable year
nrow(unique(mp['year']))
## [1] 7

There are 550 individuals (N) and 7 time periods (T)

#limit to just ID and year
mp3 <- mp %>% 
  select(c('distid', 'year'))

#unique function to drop duplicates
mp3 <- unique(mp3)

#tabulating how many times each year appears
table(mp3)
##        year
## distid  1992 1993 1994 1995 1996 1997 1998
##   1010     1    1    1    1    1    1    1
##   2010     1    1    1    1    1    1    1
##   2020     1    1    1    1    1    1    1
##   2070     1    1    1    1    1    1    1
##   2080     1    1    1    1    1    1    1
##   3000     1    1    1    1    1    1    1
##   3010     1    1    1    1    1    1    1
##   3020     1    1    1    1    1    1    1
##   3030     1    1    1    1    1    1    1
##   3040     1    1    1    1    1    1    1
##   3050     1    1    1    1    1    1    1
##   3060     1    1    1    1    1    1    1
##   3070     1    1    1    1    1    1    1
##   3080     1    1    1    1    1    1    1
##   3100     1    1    1    1    1    1    1
##   3440     1    1    1    1    1    1    1
##   4000     1    1    1    1    1    1    1
##   4010     1    1    1    1    1    1    1
##   5010     1    1    1    1    1    1    1
##   5035     1    1    1    1    1    1    1
##   5040     1    1    1    1    1    1    1
##   5060     1    1    1    1    1    1    1
##   5065     1    1    1    1    1    1    1
##   5070     1    1    1    1    1    1    1
##   6010     1    1    1    1    1    1    1
##   6020     1    1    1    1    1    1    1
##   6050     1    1    1    1    1    1    1
##   7010     1    1    1    1    1    1    1
##   7040     1    1    1    1    1    1    1
##   8010     1    1    1    1    1    1    1
##   8030     1    1    1    1    1    1    1
##   8050     1    1    1    1    1    1    1
##   9010     1    1    1    1    1    1    1
##   9030     1    1    1    1    1    1    1
##   9050     1    1    1    1    1    1    1
##   9090     1    1    1    1    1    1    1
##   10015    1    1    1    1    1    1    1
##   10025    1    1    1    1    1    1    1
##   11000    1    1    1    1    1    1    1
##   11010    1    1    1    1    1    1    1
##   11020    1    1    1    1    1    1    1
##   11030    1    1    1    1    1    1    1
##   11033    1    1    1    1    1    1    1
##   11160    1    1    1    1    1    1    1
##   11200    1    1    1    1    1    1    1
##   11210    1    1    1    1    1    1    1
##   11240    1    1    1    1    1    1    1
##   11250    1    1    1    1    1    1    1
##   11300    1    1    1    1    1    1    1
##   11310    1    1    1    1    1    1    1
##   11320    1    1    1    1    1    1    1
##   11330    1    1    1    1    1    1    1
##   11340    1    1    1    1    1    1    1
##   11670    1    1    1    1    1    1    1
##   12010    1    1    1    1    1    1    1
##   12020    1    1    1    1    1    1    1
##   12040    1    1    1    1    1    1    1
##   13000    1    1    1    1    1    1    1
##   13010    1    1    1    1    1    1    1
##   13020    1    1    1    1    1    1    1
##   13050    1    1    1    1    1    1    1
##   13070    1    1    1    1    1    1    1
##   13080    1    1    1    1    1    1    1
##   13090    1    1    1    1    1    1    1
##   13095    1    1    1    1    1    1    1
##   13110    1    1    1    1    1    1    1
##   13120    1    1    1    1    1    1    1
##   13130    1    1    1    1    1    1    1
##   13135    1    1    1    1    1    1    1
##   14010    1    1    1    1    1    1    1
##   14020    1    1    1    1    1    1    1
##   14030    1    1    1    1    1    1    1
##   14050    1    1    1    1    1    1    1
##   15000    1    1    1    1    1    1    1
##   15010    1    1    1    1    1    1    1
##   15020    1    1    1    1    1    1    1
##   15030    1    1    1    1    1    1    1
##   15050    1    1    1    1    1    1    1
##   15060    1    1    1    1    1    1    1
##   16015    1    1    1    1    1    1    1
##   16050    1    1    1    1    1    1    1
##   16070    1    1    1    1    1    1    1
##   16100    1    1    1    1    1    1    1
##   17000    1    1    1    1    1    1    1
##   17010    1    1    1    1    1    1    1
##   17050    1    1    1    1    1    1    1
##   17090    1    1    1    1    1    1    1
##   17110    1    1    1    1    1    1    1
##   17140    1    1    1    1    1    1    1
##   18010    1    1    1    1    1    1    1
##   18020    1    1    1    1    1    1    1
##   18060    1    1    1    1    1    1    1
##   19010    1    1    1    1    1    1    1
##   19070    1    1    1    1    1    1    1
##   19100    1    1    1    1    1    1    1
##   19120    1    1    1    1    1    1    1
##   19125    1    1    1    1    1    1    1
##   19140    1    1    1    1    1    1    1
##   20015    1    1    1    1    1    1    1
##   21010    1    1    1    1    1    1    1
##   21025    1    1    1    1    1    1    1
##   21060    1    1    1    1    1    1    1
##   21065    1    1    1    1    1    1    1
##   21090    1    1    1    1    1    1    1
##   21135    1    1    1    1    1    1    1
##   22010    1    1    1    1    1    1    1
##   22030    1    1    1    1    1    1    1
##   22045    1    1    1    1    1    1    1
##   23000    1    1    1    1    1    1    1
##   23010    1    1    1    1    1    1    1
##   23030    1    1    1    1    1    1    1
##   23050    1    1    1    1    1    1    1
##   23060    1    1    1    1    1    1    1
##   23065    1    1    1    1    1    1    1
##   23080    1    1    1    1    1    1    1
##   23090    1    1    1    1    1    1    1
##   24020    1    1    1    1    1    1    1
##   24030    1    1    1    1    1    1    1
##   24040    1    1    1    1    1    1    1
##   24070    1    1    1    1    1    1    1
##   25010    1    1    1    1    1    1    1
##   25030    1    1    1    1    1    1    1
##   25040    1    1    1    1    1    1    1
##   25050    1    1    1    1    1    1    1
##   25060    1    1    1    1    1    1    1
##   25070    1    1    1    1    1    1    1
##   25080    1    1    1    1    1    1    1
##   25100    1    1    1    1    1    1    1
##   25110    1    1    1    1    1    1    1
##   25120    1    1    1    1    1    1    1
##   25130    1    1    1    1    1    1    1
##   25140    1    1    1    1    1    1    1
##   25150    1    1    1    1    1    1    1
##   25180    1    1    1    1    1    1    1
##   25200    1    1    1    1    1    1    1
##   25210    1    1    1    1    1    1    1
##   25230    1    1    1    1    1    1    1
##   25240    1    1    1    1    1    1    1
##   25250    1    1    1    1    1    1    1
##   25260    1    1    1    1    1    1    1
##   25280    1    1    1    1    1    1    1
##   26010    1    1    1    1    1    1    1
##   26040    1    1    1    1    1    1    1
##   27010    1    1    1    1    1    1    1
##   27020    1    1    1    1    1    1    1
##   27060    1    1    1    1    1    1    1
##   27070    1    1    1    1    1    1    1
##   27080    1    1    1    1    1    1    1
##   28000    1    1    1    1    1    1    1
##   28010    1    1    1    1    1    1    1
##   28035    1    1    1    1    1    1    1
##   28090    1    1    1    1    1    1    1
##   29000    1    1    1    1    1    1    1
##   29010    1    1    1    1    1    1    1
##   29020    1    1    1    1    1    1    1
##   29040    1    1    1    1    1    1    1
##   29050    1    1    1    1    1    1    1
##   29060    1    1    1    1    1    1    1
##   29100    1    1    1    1    1    1    1
##   30010    1    1    1    1    1    1    1
##   30020    1    1    1    1    1    1    1
##   30030    1    1    1    1    1    1    1
##   30040    1    1    1    1    1    1    1
##   30050    1    1    1    1    1    1    1
##   30060    1    1    1    1    1    1    1
##   30070    1    1    1    1    1    1    1
##   30080    1    1    1    1    1    1    1
##   31010    1    1    1    1    1    1    1
##   31020    1    1    1    1    1    1    1
##   31030    1    1    1    1    1    1    1
##   31050    1    1    1    1    1    1    1
##   31100    1    1    1    1    1    1    1
##   31110    1    1    1    1    1    1    1
##   31130    1    1    1    1    1    1    1
##   31140    1    1    1    1    1    1    1
##   32010    1    1    1    1    1    1    1
##   32030    1    1    1    1    1    1    1
##   32050    1    1    1    1    1    1    1
##   32060    1    1    1    1    1    1    1
##   32080    1    1    1    1    1    1    1
##   32090    1    1    1    1    1    1    1
##   32130    1    1    1    1    1    1    1
##   32170    1    1    1    1    1    1    1
##   33010    1    1    1    1    1    1    1
##   33020    1    1    1    1    1    1    1
##   33040    1    1    1    1    1    1    1
##   33060    1    1    1    1    1    1    1
##   33070    1    1    1    1    1    1    1
##   33100    1    1    1    1    1    1    1
##   33130    1    1    1    1    1    1    1
##   33170    1    1    1    1    1    1    1
##   33215    1    1    1    1    1    1    1
##   33220    1    1    1    1    1    1    1
##   33230    1    1    1    1    1    1    1
##   34010    1    1    1    1    1    1    1
##   34040    1    1    1    1    1    1    1
##   34080    1    1    1    1    1    1    1
##   34090    1    1    1    1    1    1    1
##   34110    1    1    1    1    1    1    1
##   34120    1    1    1    1    1    1    1
##   34360    1    1    1    1    1    1    1
##   35010    1    1    1    1    1    1    1
##   35020    1    1    1    1    1    1    1
##   35030    1    1    1    1    1    1    1
##   35040    1    1    1    1    1    1    1
##   36015    1    1    1    1    1    1    1
##   36025    1    1    1    1    1    1    1
##   37010    1    1    1    1    1    1    1
##   37040    1    1    1    1    1    1    1
##   37060    1    1    1    1    1    1    1
##   38010    1    1    1    1    1    1    1
##   38020    1    1    1    1    1    1    1
##   38040    1    1    1    1    1    1    1
##   38050    1    1    1    1    1    1    1
##   38080    1    1    1    1    1    1    1
##   38090    1    1    1    1    1    1    1
##   38100    1    1    1    1    1    1    1
##   38120    1    1    1    1    1    1    1
##   38130    1    1    1    1    1    1    1
##   38140    1    1    1    1    1    1    1
##   38150    1    1    1    1    1    1    1
##   38170    1    1    1    1    1    1    1
##   39000    1    1    1    1    1    1    1
##   39010    1    1    1    1    1    1    1
##   39020    1    1    1    1    1    1    1
##   39030    1    1    1    1    1    1    1
##   39050    1    1    1    1    1    1    1
##   39065    1    1    1    1    1    1    1
##   39130    1    1    1    1    1    1    1
##   39140    1    1    1    1    1    1    1
##   39160    1    1    1    1    1    1    1
##   39170    1    1    1    1    1    1    1
##   40020    1    1    1    1    1    1    1
##   40040    1    1    1    1    1    1    1
##   41010    1    1    1    1    1    1    1
##   41020    1    1    1    1    1    1    1
##   41025    1    1    1    1    1    1    1
##   41026    1    1    1    1    1    1    1
##   41040    1    1    1    1    1    1    1
##   41050    1    1    1    1    1    1    1
##   41070    1    1    1    1    1    1    1
##   41080    1    1    1    1    1    1    1
##   41090    1    1    1    1    1    1    1
##   41110    1    1    1    1    1    1    1
##   41120    1    1    1    1    1    1    1
##   41130    1    1    1    1    1    1    1
##   41140    1    1    1    1    1    1    1
##   41145    1    1    1    1    1    1    1
##   41150    1    1    1    1    1    1    1
##   41160    1    1    1    1    1    1    1
##   41170    1    1    1    1    1    1    1
##   41210    1    1    1    1    1    1    1
##   41240    1    1    1    1    1    1    1
##   43040    1    1    1    1    1    1    1
##   44010    1    1    1    1    1    1    1
##   44020    1    1    1    1    1    1    1
##   44050    1    1    1    1    1    1    1
##   44060    1    1    1    1    1    1    1
##   44090    1    1    1    1    1    1    1
##   45010    1    1    1    1    1    1    1
##   45020    1    1    1    1    1    1    1
##   45040    1    1    1    1    1    1    1
##   45050    1    1    1    1    1    1    1
##   46010    1    1    1    1    1    1    1
##   46020    1    1    1    1    1    1    1
##   46040    1    1    1    1    1    1    1
##   46050    1    1    1    1    1    1    1
##   46060    1    1    1    1    1    1    1
##   46070    1    1    1    1    1    1    1
##   46080    1    1    1    1    1    1    1
##   46090    1    1    1    1    1    1    1
##   46100    1    1    1    1    1    1    1
##   46110    1    1    1    1    1    1    1
##   46130    1    1    1    1    1    1    1
##   46140    1    1    1    1    1    1    1
##   47010    1    1    1    1    1    1    1
##   47030    1    1    1    1    1    1    1
##   47060    1    1    1    1    1    1    1
##   47070    1    1    1    1    1    1    1
##   47080    1    1    1    1    1    1    1
##   48040    1    1    1    1    1    1    1
##   49010    1    1    1    1    1    1    1
##   49040    1    1    1    1    1    1    1
##   49055    1    1    1    1    1    1    1
##   49070    1    1    1    1    1    1    1
##   49110    1    1    1    1    1    1    1
##   50000    1    1    1    1    1    1    1
##   50010    1    1    1    1    1    1    1
##   50020    1    1    1    1    1    1    1
##   50030    1    1    1    1    1    1    1
##   50040    1    1    1    1    1    1    1
##   50050    1    1    1    1    1    1    1
##   50070    1    1    1    1    1    1    1
##   50080    1    1    1    1    1    1    1
##   50090    1    1    1    1    1    1    1
##   50100    1    1    1    1    1    1    1
##   50120    1    1    1    1    1    1    1
##   50130    1    1    1    1    1    1    1
##   50140    1    1    1    1    1    1    1
##   50160    1    1    1    1    1    1    1
##   50170    1    1    1    1    1    1    1
##   50180    1    1    1    1    1    1    1
##   50190    1    1    1    1    1    1    1
##   50200    1    1    1    1    1    1    1
##   50210    1    1    1    1    1    1    1
##   50220    1    1    1    1    1    1    1
##   50230    1    1    1    1    1    1    1
##   50240    1    1    1    1    1    1    1
##   51020    1    1    1    1    1    1    1
##   51045    1    1    1    1    1    1    1
##   51060    1    1    1    1    1    1    1
##   51070    1    1    1    1    1    1    1
##   52015    1    1    1    1    1    1    1
##   52040    1    1    1    1    1    1    1
##   52090    1    1    1    1    1    1    1
##   52100    1    1    1    1    1    1    1
##   52110    1    1    1    1    1    1    1
##   52160    1    1    1    1    1    1    1
##   52170    1    1    1    1    1    1    1
##   52180    1    1    1    1    1    1    1
##   53010    1    1    1    1    1    1    1
##   53020    1    1    1    1    1    1    1
##   53030    1    1    1    1    1    1    1
##   53040    1    1    1    1    1    1    1
##   54000    1    1    1    1    1    1    1
##   54010    1    1    1    1    1    1    1
##   54025    1    1    1    1    1    1    1
##   54040    1    1    1    1    1    1    1
##   55000    1    1    1    1    1    1    1
##   55010    1    1    1    1    1    1    1
##   55100    1    1    1    1    1    1    1
##   55115    1    1    1    1    1    1    1
##   55120    1    1    1    1    1    1    1
##   56010    1    1    1    1    1    1    1
##   56020    1    1    1    1    1    1    1
##   56030    1    1    1    1    1    1    1
##   56050    1    1    1    1    1    1    1
##   57020    1    1    1    1    1    1    1
##   57030    1    1    1    1    1    1    1
##   58010    1    1    1    1    1    1    1
##   58020    1    1    1    1    1    1    1
##   58030    1    1    1    1    1    1    1
##   58050    1    1    1    1    1    1    1
##   58070    1    1    1    1    1    1    1
##   58080    1    1    1    1    1    1    1
##   58090    1    1    1    1    1    1    1
##   58100    1    1    1    1    1    1    1
##   58110    1    1    1    1    1    1    1
##   59020    1    1    1    1    1    1    1
##   59045    1    1    1    1    1    1    1
##   59070    1    1    1    1    1    1    1
##   59080    1    1    1    1    1    1    1
##   59090    1    1    1    1    1    1    1
##   59125    1    1    1    1    1    1    1
##   59150    1    1    1    1    1    1    1
##   60010    1    1    1    1    1    1    1
##   60020    1    1    1    1    1    1    1
##   61000    1    1    1    1    1    1    1
##   61010    1    1    1    1    1    1    1
##   61020    1    1    1    1    1    1    1
##   61060    1    1    1    1    1    1    1
##   61065    1    1    1    1    1    1    1
##   61080    1    1    1    1    1    1    1
##   61180    1    1    1    1    1    1    1
##   61190    1    1    1    1    1    1    1
##   61210    1    1    1    1    1    1    1
##   61220    1    1    1    1    1    1    1
##   61230    1    1    1    1    1    1    1
##   61240    1    1    1    1    1    1    1
##   62040    1    1    1    1    1    1    1
##   62050    1    1    1    1    1    1    1
##   62060    1    1    1    1    1    1    1
##   62070    1    1    1    1    1    1    1
##   62090    1    1    1    1    1    1    1
##   62470    1    1    1    1    1    1    1
##   63000    1    1    1    1    1    1    1
##   63010    1    1    1    1    1    1    1
##   63020    1    1    1    1    1    1    1
##   63030    1    1    1    1    1    1    1
##   63040    1    1    1    1    1    1    1
##   63050    1    1    1    1    1    1    1
##   63060    1    1    1    1    1    1    1
##   63070    1    1    1    1    1    1    1
##   63080    1    1    1    1    1    1    1
##   63090    1    1    1    1    1    1    1
##   63100    1    1    1    1    1    1    1
##   63110    1    1    1    1    1    1    1
##   63130    1    1    1    1    1    1    1
##   63140    1    1    1    1    1    1    1
##   63150    1    1    1    1    1    1    1
##   63160    1    1    1    1    1    1    1
##   63180    1    1    1    1    1    1    1
##   63190    1    1    1    1    1    1    1
##   63200    1    1    1    1    1    1    1
##   63210    1    1    1    1    1    1    1
##   63220    1    1    1    1    1    1    1
##   63230    1    1    1    1    1    1    1
##   63240    1    1    1    1    1    1    1
##   63250    1    1    1    1    1    1    1
##   63260    1    1    1    1    1    1    1
##   63270    1    1    1    1    1    1    1
##   63280    1    1    1    1    1    1    1
##   63290    1    1    1    1    1    1    1
##   63300    1    1    1    1    1    1    1
##   64000    1    1    1    1    1    1    1
##   64040    1    1    1    1    1    1    1
##   64070    1    1    1    1    1    1    1
##   64080    1    1    1    1    1    1    1
##   64090    1    1    1    1    1    1    1
##   65045    1    1    1    1    1    1    1
##   66045    1    1    1    1    1    1    1
##   66050    1    1    1    1    1    1    1
##   66070    1    1    1    1    1    1    1
##   67020    1    1    1    1    1    1    1
##   67050    1    1    1    1    1    1    1
##   67055    1    1    1    1    1    1    1
##   67060    1    1    1    1    1    1    1
##   68010    1    1    1    1    1    1    1
##   68030    1    1    1    1    1    1    1
##   69020    1    1    1    1    1    1    1
##   69030    1    1    1    1    1    1    1
##   69040    1    1    1    1    1    1    1
##   70010    1    1    1    1    1    1    1
##   70020    1    1    1    1    1    1    1
##   70040    1    1    1    1    1    1    1
##   70070    1    1    1    1    1    1    1
##   70120    1    1    1    1    1    1    1
##   70175    1    1    1    1    1    1    1
##   70300    1    1    1    1    1    1    1
##   70350    1    1    1    1    1    1    1
##   71050    1    1    1    1    1    1    1
##   71060    1    1    1    1    1    1    1
##   71080    1    1    1    1    1    1    1
##   72000    1    1    1    1    1    1    1
##   72010    1    1    1    1    1    1    1
##   72020    1    1    1    1    1    1    1
##   73010    1    1    1    1    1    1    1
##   73030    1    1    1    1    1    1    1
##   73040    1    1    1    1    1    1    1
##   73080    1    1    1    1    1    1    1
##   73110    1    1    1    1    1    1    1
##   73170    1    1    1    1    1    1    1
##   73180    1    1    1    1    1    1    1
##   73190    1    1    1    1    1    1    1
##   73200    1    1    1    1    1    1    1
##   73210    1    1    1    1    1    1    1
##   73230    1    1    1    1    1    1    1
##   73240    1    1    1    1    1    1    1
##   73255    1    1    1    1    1    1    1
##   74000    1    1    1    1    1    1    1
##   74010    1    1    1    1    1    1    1
##   74030    1    1    1    1    1    1    1
##   74040    1    1    1    1    1    1    1
##   74050    1    1    1    1    1    1    1
##   74100    1    1    1    1    1    1    1
##   74120    1    1    1    1    1    1    1
##   74130    1    1    1    1    1    1    1
##   75010    1    1    1    1    1    1    1
##   75020    1    1    1    1    1    1    1
##   75030    1    1    1    1    1    1    1
##   75040    1    1    1    1    1    1    1
##   75050    1    1    1    1    1    1    1
##   75060    1    1    1    1    1    1    1
##   75070    1    1    1    1    1    1    1
##   75080    1    1    1    1    1    1    1
##   75100    1    1    1    1    1    1    1
##   76060    1    1    1    1    1    1    1
##   76070    1    1    1    1    1    1    1
##   76080    1    1    1    1    1    1    1
##   76090    1    1    1    1    1    1    1
##   76140    1    1    1    1    1    1    1
##   76180    1    1    1    1    1    1    1
##   76210    1    1    1    1    1    1    1
##   77010    1    1    1    1    1    1    1
##   78020    1    1    1    1    1    1    1
##   78030    1    1    1    1    1    1    1
##   78040    1    1    1    1    1    1    1
##   78060    1    1    1    1    1    1    1
##   78070    1    1    1    1    1    1    1
##   78080    1    1    1    1    1    1    1
##   78100    1    1    1    1    1    1    1
##   78110    1    1    1    1    1    1    1
##   79010    1    1    1    1    1    1    1
##   79020    1    1    1    1    1    1    1
##   79030    1    1    1    1    1    1    1
##   79080    1    1    1    1    1    1    1
##   79090    1    1    1    1    1    1    1
##   79100    1    1    1    1    1    1    1
##   79110    1    1    1    1    1    1    1
##   79145    1    1    1    1    1    1    1
##   79150    1    1    1    1    1    1    1
##   80000    1    1    1    1    1    1    1
##   80010    1    1    1    1    1    1    1
##   80020    1    1    1    1    1    1    1
##   80050    1    1    1    1    1    1    1
##   80090    1    1    1    1    1    1    1
##   80110    1    1    1    1    1    1    1
##   80120    1    1    1    1    1    1    1
##   80130    1    1    1    1    1    1    1
##   80140    1    1    1    1    1    1    1
##   80150    1    1    1    1    1    1    1
##   80160    1    1    1    1    1    1    1
##   81000    1    1    1    1    1    1    1
##   81010    1    1    1    1    1    1    1
##   81020    1    1    1    1    1    1    1
##   81040    1    1    1    1    1    1    1
##   81050    1    1    1    1    1    1    1
##   81070    1    1    1    1    1    1    1
##   81080    1    1    1    1    1    1    1
##   81100    1    1    1    1    1    1    1
##   81120    1    1    1    1    1    1    1
##   81140    1    1    1    1    1    1    1
##   81150    1    1    1    1    1    1    1
##   82010    1    1    1    1    1    1    1
##   82020    1    1    1    1    1    1    1
##   82030    1    1    1    1    1    1    1
##   82040    1    1    1    1    1    1    1
##   82045    1    1    1    1    1    1    1
##   82050    1    1    1    1    1    1    1
##   82055    1    1    1    1    1    1    1
##   82060    1    1    1    1    1    1    1
##   82070    1    1    1    1    1    1    1
##   82080    1    1    1    1    1    1    1
##   82090    1    1    1    1    1    1    1
##   82095    1    1    1    1    1    1    1
##   82100    1    1    1    1    1    1    1
##   82110    1    1    1    1    1    1    1
##   82120    1    1    1    1    1    1    1
##   82130    1    1    1    1    1    1    1
##   82140    1    1    1    1    1    1    1
##   82150    1    1    1    1    1    1    1
##   82155    1    1    1    1    1    1    1
##   82160    1    1    1    1    1    1    1
##   82170    1    1    1    1    1    1    1
##   82180    1    1    1    1    1    1    1
##   82230    1    1    1    1    1    1    1
##   82240    1    1    1    1    1    1    1
##   82250    1    1    1    1    1    1    1
##   82290    1    1    1    1    1    1    1
##   82300    1    1    1    1    1    1    1
##   82320    1    1    1    1    1    1    1
##   82340    1    1    1    1    1    1    1
##   82365    1    1    1    1    1    1    1
##   82390    1    1    1    1    1    1    1
##   82400    1    1    1    1    1    1    1
##   82405    1    1    1    1    1    1    1
##   82430    1    1    1    1    1    1    1
##   83010    1    1    1    1    1    1    1
##   83060    1    1    1    1    1    1    1
##   83070    1    1    1    1    1    1    1

The table method is what Nick recommended, but since there are 550 participants we can only see the first 142 in the table- all of those appear equally in each year, however, so I believe we can consider this a balanced panel.

  1. OLS regression:
m1 <- lm(math4 ~ expp + lunch, data = mp)
tidy(m1)
## # A tibble: 3 × 5
##   term        estimate std.error statistic   p.value
##   <chr>          <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept) 29.5      1.14          25.9 1.12e-136
## 2 expp         0.00692  0.000199      34.7 7.75e-230
## 3 lunch       -0.381    0.0159       -24.0 1.02e-118
#copying dataset so I don't write over the variables
mp5 <- mp

mp5 <- mp5 %>% 
  mutate(math4_demean= math4 - mean(math4), expp_demean= expp - mean(expp), lunch_demean= lunch - mean(lunch))

#regression
m2 <- lm(math4_demean ~ expp_demean + lunch_demean, data = mp5)
tidy(m2)
## # A tibble: 3 × 5
##   term          estimate std.error statistic   p.value
##   <chr>            <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)   3.06e-14  0.244     1.26e-13 1.00e+  0
## 2 expp_demean   6.92e- 3  0.000199  3.47e+ 1 7.75e-230
## 3 lunch_demean -3.81e- 1  0.0159   -2.40e+ 1 1.02e-118
#first 500 observations
mp6 <- head(mp, 500)

#dummies for distid using factor command
m3 <- lm(math4 ~ expp + lunch + factor(distid), data = mp6)

#checking that distid worked/that there is more than 1 distid coefficient
tidy(m3)
## # A tibble: 74 × 5
##    term                estimate std.error statistic  p.value
##    <chr>                  <dbl>     <dbl>     <dbl>    <dbl>
##  1 (Intercept)        -29.7      8.78        -3.38  8.02e- 4
##  2 expp                 0.00884  0.000655    13.5   9.07e-35
##  3 lunch                0.801    0.193        4.14  4.14e- 5
##  4 factor(distid)2010 -12.9      7.05        -1.83  6.82e- 2
##  5 factor(distid)2020 -38.5      7.50        -5.14  4.23e- 7
##  6 factor(distid)2070  30.6      7.34         4.17  3.70e- 5
##  7 factor(distid)2080  -1.47     6.90        -0.213 8.32e- 1
##  8 factor(distid)3000  31.4      7.76         4.05  6.09e- 5
##  9 factor(distid)3010  32.1      8.35         3.84  1.43e- 4
## 10 factor(distid)3020  38.7      7.81         4.95  1.06e- 6
## # … with 64 more rows
#joint F test but I am not 100% sure this is correct since even though there are many levels of the dummy variable since I'm only writing in one variable for the argument it doesn't seem correct?
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
#have to transform factor(distid) into a vector since I kept getting error messages
vector <- m3$coefficients
vector2 <- vector[-c(1:3)] #getting rid of the first 3 coefficients since they are not the dummy vars we're testing
vector3 <- as.vector(vector2)

#I keep getting the error 'non-conformable arguments' and the internet said it might be because I am using this as a vector instead of a matrix so I will try to convert this to a matrix and try again

matrix <- m3$coefficients[4:length(m3$coefficients)]

#that also didn't work. I'm just gonna try the messier version where I specify the "matrix" within the code

linearHypothesis(m3, hypothesis.matrix = m3$coefficients, test = "F")
## Linear hypothesis test
## 
## Hypothesis:
## - 29.6590783243019 ((Intercept)  + 0.00883871591758724 expp  + 0.800728644366392 lunch - 12.8879059033503 factor(distid)2010 - 38.5196544371994 factor(distid)2020  + 30.6091347553316 factor(distid)2070 - 1.46665356902002 factor(distid)2080  + 31.4463924541087 factor(distid)3000  + 32.052098432778 factor(distid)3010  + 38.6608643376359 factor(distid)3020  + 26.2832218272459 factor(distid)3030  + 34.5786957133566 factor(distid)3040  + 1.24305817430685 factor(distid)3050  + 15.596003467916 factor(distid)3060  + 25.2889232993897 factor(distid)3070  + 11.4043721372268 factor(distid)3080  + 38.5346518567922 factor(distid)3100  + 48.6522447586899 factor(distid)3440  + 14.6409064466701 factor(distid)4000  + 18.4423832480221 factor(distid)4010 - 34.1619823297569 factor(distid)5010  + 11.5116176266702 factor(distid)5035  + 36.1782571822726 factor(distid)5040  + 33.8871883053288 factor(distid)5060  + 12.2009238446774 factor(distid)5065  + 0.938891448507719 factor(distid)5070 - 15.378790498365 factor(distid)6010  + 16.4756662976466 factor(distid)6020  + 9.03698196566973 factor(distid)6050 - 12.45492010021 factor(distid)7010 - 1.2219777186993 factor(distid)7040  + 24.9964308467042 factor(distid)8010  + 31.3272814806374 factor(distid)8030  + 27.0426339455797 factor(distid)8050  + 10.5417537020679 factor(distid)9010  + 32.6087861823465 factor(distid)9030  + 28.5889567239893 factor(distid)9050  + 15.7557579626306 factor(distid)9090  + 13.9161164844713 factor(distid)10015  + 14.4193951009244 factor(distid)10025  + 21.3318636512815 factor(distid)11000 - 50.1281068257429 factor(distid)11010  + 44.8208217680869 factor(distid)11020  + 44.5425811882176 factor(distid)11030  + 31.7720089091413 factor(distid)11033  + 10.8936157018579 factor(distid)11160  + 3.07593243152185 factor(distid)11200  + 37.2619117837213 factor(distid)11210  + 11.1208581721842 factor(distid)11240 - 3.95194484928107 factor(distid)11250  + 14.4902185746624 factor(distid)11300  + 28.2010523942371 factor(distid)11310  + 15.5796469637254 factor(distid)11320  + 2.58389083377856 factor(distid)11330  + 8.63917974142514 factor(distid)11340  + 30.838247394859 factor(distid)11670  + 16.742119597381 factor(distid)12010  + 7.00961249938221 factor(distid)12020  + 13.3202861812024 factor(distid)12040  + 20.026432011715 factor(distid)13000 - 24.6506994627853 factor(distid)13010 - 29.0996677521167 factor(distid)13020  + 18.3614546071834 factor(distid)13050  + 34.7181123284492 factor(distid)13070  + 25.2021249057687 factor(distid)13080  + 31.1293255226449 factor(distid)13090  + 12.2366996554301 factor(distid)13095  + 35.664879014261 factor(distid)13110  + 29.1802027911551 factor(distid)13120  + 16.8000537453833 factor(distid)13130  + 21.9505322645895 factor(distid)13135  + 2.74041697874333 factor(distid)14010  + 17.0680686993033 factor(distid)14020  + 15.1367845997179 factor(distid)14030 = 0
## 
## Model 1: restricted model
## Model 2: math4 ~ expp + lunch + factor(distid)
## 
##   Res.Df   RSS Df Sum of Sq      F    Pr(>F)    
## 1    427 76454                                  
## 2    426 70858  1    5596.4 33.646 1.291e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#THAT WORKED !!!

Statistically significant!

library(fixest)
#feols command
m4 <- feols(math4 ~ expp + lunch | distid, data = mp)
tidy(m4)
## # A tibble: 2 × 5
##   term  estimate std.error statistic  p.value
##   <chr>    <dbl>     <dbl>     <dbl>    <dbl>
## 1 expp    0.0120  0.000486     24.7  2.54e-91
## 2 lunch   0.314   0.0940        3.34 9.10e- 4
  1. Similar to the last one
m5 <- feols(math4 ~ expp + lunch | distid + year, data = mp) #since SE clusters around the first fixed effect, since distid is the first one here we don't have to add any additional arguments
tidy(m5)
## # A tibble: 2 × 5
##   term   estimate std.error statistic p.value
##   <chr>     <dbl>     <dbl>     <dbl>   <dbl>
## 1 expp  -0.000168  0.000710    -0.237   0.813
## 2 lunch  0.0184    0.100        0.184   0.854
#using coef_map to specify the variables we want in the table
modelsummary(models = list(m1, m2, m3, m4, m5),
             coef_map = c('expp_demean',
                          'lunch_demean',
                          'expp',
                          'lunch'))
Model 1 Model 2 Model 3 Model 4 Model 5
expp_demean 0.007
(0.000)
lunch_demean −0.381
(0.016)
expp 0.007 0.009 0.012 0.000
(0.000) (0.001) (0.000) (0.001)
lunch −0.381 0.801 0.314 0.018
(0.016) (0.193) (0.094) (0.100)
Num.Obs. 3850 3850 500 3850 3850
R2 0.308 0.308 0.551 0.680 0.793
R2 Adj. 0.307 0.307 0.474 0.626 0.758
R2 Within 0.499 0.000
R2 Pseudo
AIC 31856.6 31856.6 4045.8 29984.4 28319.5
BIC 31881.6 31881.6 4361.9 33437.6 31810.3
Log.Lik. −15924.294 −15924.294 −1947.924 −14440.199 −13601.767
F 855.419 855.419 7.162
Std.Errors by: distid by: distid
FE: distid X X
FE: year X

The first interesting thing I’m noticing is that regardless of model (including the one where we have the ‘demean’ version of each coefficient), the expp coefficient is close to (or equal to) zero so we can be pretty confident that there either is no effect from that variable or that the effect is very very small.

The second interesting thing I’m noticing here is that the R2 and R2 adjusted are much higher for the last two models (with fixed effects), suggesting that the fixed effects had a large influence on how much these models were able to explain the variability within the dependent variable.

#mutate to expp and lunch mean and have distid factor version
mp10 <- mp5 %>% 
  mutate(expp_mean= mean(expp), lunch_mean= mean(lunch), distid= factor(distid))

m6 <- lmer(math4 ~ expp_demean + lunch_demean + expp_mean + lunch_mean | distid, data = mp10)
## boundary (singular) fit: see help('isSingular')
modelsummary(m6)
Model 1
(Intercept) 52.594
(10.848)
sd__(Intercept) 9.654
cor__(Intercept).expp_demean 0.383
cor__(Intercept).lunch_demean 0.227
cor__(Intercept).expp_mean 0.422
cor__(Intercept).lunch_mean −0.050
sd__expp_demean 0.015
cor__expp_demean.lunch_demean 0.646
cor__expp_demean.expp_mean 0.824
cor__expp_demean.lunch_mean −0.032
sd__lunch_demean 0.151
cor__lunch_demean.expp_mean 0.189
cor__lunch_demean.lunch_mean −0.016
sd__expp_mean 0.002
cor__expp_mean.lunch_mean −0.034
sd__lunch_mean 9.361
sd__Observation 9.366
AIC 34228.3
BIC 34334.6
Log.Lik. −17097.146
REMLcrit 34194.291